#include "ParseTree.h"
#include <stdio.h>
#include <assert.h>


int ParseTree::nextObjectID = 0;

ParseTree::ParseTree() :
    firstChild(NULL),
    lastChild(NULL),
    nextSibling(NULL),
    objectID(nextObjectID++),
    type(TYPE_NO),
    nrOfChilds(0)
{
    //assert(objectID != 1);
}

ParseTree::~ParseTree(){}

ParseTree* ParseTree::getNextSibling()
{
    return this->nextSibling;
}

void ParseTree::setNextSibling(ParseTree* nextSibling)
{
    this->nextSibling= nextSibling;
}

ParseTree* ParseTree::getFirstChild()
{
    return firstChild;
}

void ParseTree::add_node(ParseTree* newChild)
{
    if(firstChild == NULL || lastChild == NULL){//if its the first child
        assert(firstChild == lastChild);
        firstChild = newChild;
        lastChild  = newChild;
    } else{
        lastChild->setNextSibling(newChild);
        lastChild = newChild;
    }
    nrOfChilds++;
}

void ParseTree::create(void (*typeCheckFunc) (ParseTree*), void (*makeCodeFunc) (ParseTree*))
{
    this->typeCheckFunc = typeCheckFunc;
    this->makeCodeFunc  = makeCodeFunc;
}

TType ParseTree::getType()
{
    return type;
}

void ParseTree::setType(TType type)
{
   this->type = type;
}

void ParseTree::typeCheck()
{
    if(typeCheckFunc == NULL){
        printf("###ERR invalid reference to typeCheck function in object nr %d\n", objectID);
        assert(false);
    } //else:
    (void) (*typeCheckFunc)(this);
}

void ParseTree::makeCode()
{
    if(makeCodeFunc == NULL){
        printf("###ERR invalid reference to makeCode function in object nr %d\n", objectID);
        assert(false);
    } //else:
    (void) (*makeCodeFunc)(this);
}
